package com.heima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.PageResult;
import com.heima.mapper.PartnerMapper;
import com.heima.mapper.VmMapper;
import com.heima.pojo.Partner;
import com.heima.pojo.Vm;
import com.heima.pojo.dto.PartnerDTO;
import com.heima.pojo.dto.PartnerPageQueryDTO;
import com.heima.pojo.vo.PartnerVo;
import com.heima.service.PartnerService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.ArrayList;
import java.util.List;

import static com.baomidou.mybatisplus.core.toolkit.Wrappers.lambdaQuery;

@Service
public class PartnerServiceImpl implements PartnerService {
    @Autowired
    private PartnerMapper partnerMapper;

    @Autowired
    private VmMapper vmMapper;

    @Override
    public void save(PartnerDTO partnerDTO) {
        Partner partner = new Partner();
        BeanUtils.copyProperties(partnerDTO,partner);
        //加密
        partner.setPassword(DigestUtils.md5DigestAsHex(partner.getPassword().getBytes()));
        partnerMapper.insert(partner);
    }

    /**
     * 分页查询
     * @param dto
     * @return
     */
    @Override
    @Transactional
    public PageResult pageQuery(PartnerPageQueryDTO dto) {
        //分页参数
        Page<Partner> page = Page.of(dto.getPageIndex(), dto.getPageSize());
        //准备查询条件
        LambdaQueryWrapper<Partner> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(dto.getName()!=null ,Partner::getName, dto.getName());

        //执行分页查询
        Page<Partner> partnerPage = partnerMapper.selectPage(page, wrapper);
        List<Partner> partnerList = partnerPage.getRecords();
        List<PartnerVo> partnerVos = new ArrayList<>();
        for (Partner partner : partnerList) {
            LambdaQueryWrapper<Vm> vmWrapper = new LambdaQueryWrapper<>();
            vmWrapper.eq(Vm::getOwnerId,partner.getId());
            PartnerVo partnerVo = new PartnerVo();
            BeanUtils.copyProperties(partner,partnerVo);
            partnerVo.setVmCount(Integer.parseInt(vmMapper.selectCount(vmWrapper).toString()));
            partnerVos.add(partnerVo);
        }

        PageResult pageResult = new PageResult(dto.getPageIndex(),dto.getPageSize(),partnerPage.getTotal(),partnerVos);

        return pageResult;
    }

    @Override
    public PartnerVo selectById(Long id) {
        Partner partner = partnerMapper.selectById(id);
        PartnerVo partnerVo = new PartnerVo();
        BeanUtils.copyProperties(partner,partnerVo);
        LambdaQueryWrapper<Vm> vmWrapper = new LambdaQueryWrapper<>();
        vmWrapper.eq(Vm::getOwnerId,partner.getId());
        Long count = vmMapper.selectCount(vmWrapper);
        partnerVo.setVmCount(Integer.parseInt(count.toString()));
        return partnerVo;
    }

    @Override
    public void update(Long id,Partner partner) {
        partner.setId(id);
        partnerMapper.updateById(partner);
    }

    @Override
    public void delete(Long id) {
        partnerMapper.deleteById(id);
    }
}
